#include<iostream>
#include<cmath>
#include<list>
using namespace std;

#define int long long

int n,m,k,q;
int at[100010],a[100010],b[100010],c[100010];
bool d[30][100010];

signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    k=ceil(log2(n));
    for(int i=1;i<=n;++i){
        cin>>at[i];
    }
    for(int i=1;i<=m;++i){
        cin>>c[i];
    }
    for(int i=1;i<=k;++i){
        for(int j=1;j<=(1<<(k-i));++j){
            char c;cin>>c;
            d[i][j]=c-'0';
        }
    }
    cin>>q;
    while(q--){
        int x[4];
        cin>>x[0]>>x[1]>>x[2]>>x[3];
        for(int i=1;i<=n;++i){
            a[i]=at[i]^x[i%4];
            //cout<<a[i]<<' ';
        }
        //cout<<"\n";
        int ans=0;
        for(int t=1;t<=m;++t){
            k=ceil(log2(c[t]));
            for(int i=1;i<=(1<<k);++i){
                b[i]=a[i];
            }
            for(int i=1;i<=k;++i){
                for(int j=1,id=1;j<=(1<<k);++j){
                    if(b[j]==0)continue;
                    int h=j+1;
                    while(b[h]==0)h++;
                    if(d[i][id]==0){
                        if(b[j]>=i){
                            b[h]=0;
                        }
                        else{
                            b[j]=0;
                        }
                    }
                    else{
                        if(b[h]>=i){
                            b[j]=0;
                        }
                        else{
                            b[h]=0;
                        }
                    }
                    j=h;
                    id++;
                }
            }
            int tmp=0;
            for(int i=1;i<=(1<<k);++i){
                if(b[i]){
                    tmp=i;
                    break;
                }
            }
            //cout<<tmp<<endl;
            ans^=(t*tmp);
        }
        cout<<ans<<"\n";
    }
}
